package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("shoppingCartController")
@RequestMapping("/user/shoppingCart")
@Slf4j

public class shoppingCartController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加一个商品
    @PostMapping("/add")
    public Result Add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车：{}",shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);

        return Result.success();
    }

    //查询购物车
    @GetMapping("/list")
    public Result<List<ShoppingCart>> List(){

        log.info("查询购物车");
        List<ShoppingCart> shoppingCartShow = shoppingCartService.list();
        return Result.success(shoppingCartShow);
    }

    //删除一个商品
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车：{}",shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    //清空购物车
    @DeleteMapping("/clean")
    public Result clean(){
        log.info("清空购物车");
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }



}
